package com.hnucm.cooklife.model;

import android.annotation.SuppressLint;

import com.blankj.utilcode.util.NetworkUtils;
import com.hnucm.cooklife.base.BaseModel;
import com.hnucm.cooklife.bean.db.User;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.contract.user.Contract;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserModel extends BaseModel implements Contract.IUserModel {
    @SuppressLint("CheckResult")
    @Override
    public Observable<User> loadUser(int userId) {

        Observable<User> loadFromLocal = Observable.create(emitter -> {
            User user = LitePal.find(User.class, userId);
            emitter.onNext(user);
            emitter.onComplete();
        });

        if(NetworkUtils.isConnected()){
            Observable<User> loadFromNet = loadFromNet(userId);
            return loadFromNet;
        } else {
            return loadFromLocal;
        }

//        return mApiServer.loadUser(userId)
//                .map(userData -> {
//                    User user = new User();
//                    user.id = userData.getData().getUserId();
//                    user.username = userData.getData().getUsername();
//                    user.password = userData.getData().getPassword();
//                    user.icon = userData.getData().getIcon();
//                    user.bg = userData.getData().getBg();
//                    return user;
//                });
    }

    private Observable<User> loadFromNet(int userId) {
        return mApiServer.loadUser(userId)
                .map(userData -> {
                    User user = new User();
                    user.userId = userData.getData().getUserId();
                    user.username = userData.getData().getUsername();
                    user.password = userData.getData().getPassword();
                    user.icon = userData.getData().getIcon();
                    user.bg = userData.getData().getBg();
//                    if(user.id !=0){
//                        LitePal.deleteAll(User.class);
//                    }
//                    List<User> userList = new ArrayList<>();
//                    userList.add(user);
//                    LitePal.saveAll(userList);
                    return user;
                });
    }

    @Override
    public Observable<User> uploadUser(int userId) {
        return loadUser(userId);
    }

    @Override
    public Observable<User> updateIcon(MultipartBody.Part icon, int userId) {
        return mApiServer.updateUserIcon(icon, userId)
                .map(userData -> {
                    User user = new User();
//                    user.id = userData.getData().getUserId();
                    user.username = userData.getData().getUsername();
                    user.password = userData.getData().getPassword();
                    user.icon = userData.getData().getIcon();
                    user.bg = userData.getData().getBg();
                    return user;
                });
    }

    @Override
    public Observable<User> updateBg(MultipartBody.Part bg, int userId) {
        return mApiServer.updateUserBg(bg, userId)
                .map(userData -> {
                    User user = new User();
                    user.userId = userData.getData().getUserId();
                    user.username = userData.getData().getUsername();
                    user.password = userData.getData().getPassword();
                    user.icon = userData.getData().getIcon();
                    user.bg = userData.getData().getBg();
                    return user;
                });
    }



    @Override
    public Observable<List<DishColl>> loadRealMyDish(int userid) {
        return mApiServer.loadRealMyDish(userid)
                .map(realDishData -> {
                    List<DishColl> dishCollList = new ArrayList<>();
                    realDishData.getData().stream().forEach(dataDTO -> {
                        DishColl dishColl = new DishColl();
                        dishColl.dishId = dataDTO.getDishId();
                        dishColl.dishdone = dataDTO.getDishdone();
                        dishColl.dishscore = dataDTO.getDishscore();
                        dishColl.dishpic = dataDTO.getDishpic();
                        dishColl.dishname = dataDTO.getDishname();
                        dishColl.collect = dataDTO.getCollect();
                        dishColl.dishdesc = dataDTO.getDishdesc();
                        dishColl.icon = dataDTO.getIcon();
                        dishColl.userid = dataDTO.getUserid();
                        dishCollList.add(dishColl);
                    });
                    return dishCollList;
                });
    }

    @Override
    public Observable<List<DishColl>> refreshRealMyDish(int userid) {
        return null;
    }
}
